﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.Unity;

namespace LazyRabbit.Infrastructure.Dependency
{
    public class UnityDependencyResolver : IDependencyResolver
    {
        public IUnityContainer _UnityContainer;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="unityContainer">依赖注入容器</param>
        public UnityDependencyResolver(IUnityContainer unityContainer)
        {
            _UnityContainer = unityContainer;
        }

        #region IDependencyResolver 成员

        public object GetService(Type serviceType)
        {
            try
            {
                return _UnityContainer.Resolve(serviceType);
            }
            catch
            {
                /// 按微软的要求，此方法，在没有解析到任何对象的情况下，必须返回 null，必须这么做！！！！
                return null;
            }

        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return _UnityContainer.ResolveAll(serviceType);
            }
            catch
            {
                /// 按微软的要求，此方法，在没有解析到任何对象的情况下，必须返回空集合，必须这么做！！！！
                return new List<object>();
            }
        }

        #endregion
    }
}
